#
# heavily inspired by NVidia sources
#


CUDA_INSTALL_PATH := /usr/local/cuda

.SUFFIXES : .cuh .cu .cu_dbg.o .c_dbg.o .cpp_dbg.o .cu_rel.o .c_rel.o .cpp_rel.o .cubin .ptx
#SM_VERSIONS := sm_10 sm_11 sm_12 sm_13

VERBOSE :=

NVCC := nvcc
CXX  := g++
CC   := gcc
LINK := g++ -fPIC


INCLUDES  += -I../include -I. -I$(CUDA_INSTALL_PATH)/include

# necessary on latest GCC
#NVCCFLAGS := --compiler-options -fno-inline
NVCCFLAGS :=
CXXFLAGS  := 
CFLAGS    := 

COMMONFLAGS := $(INCLUDES) -DUNIX

ifeq ($(dbg),1)
	COMMONFLAGS += -g -D_DEBUG
	CXXFLAGS    += 
	CFLAGS      += 
	NVCCFLAGS   +=
	LIBSUFFIX   := D
else
	COMMONFLAGS += -O2 -DNODEBUG
	CXXFLAGS    += -ffast-math -fno-strict-aliasing
	CFLAGS      += -ffast-math -fno-strict-aliasing
	NVCCFLAGS   += -use_fast_math --compiler-options -ffast-math --compiler-options -fno-strict-aliasing
	NVCCFLAGS   += 
	LIBSUFFIX   :=
endif

# only on CUDA3.0
# NVCCFLAGS += -ftz=true
# NVCCFLAGS += -prec-div=false -prec-sqrt=false
#
# NVCCFLAGS += -Xptxas -v
# NVCCFLAGS += -maxrregcount 16
# NVCCFLAGS += --keep
# NVCCFLAGS += --device-debug=2
NVCCFLAGS += --ptxas-options=-v

SMVERSIONFLAGS:=-arch=sm_13

NVCCFLAGS += $(COMMONFLAGS)
CXXFLAGS  += $(COMMONFLAGS)
CFLAGS    += $(COMMONFLAGS)

#TODO 32/64bit auto-detection
#LIB       := -L$(CUDA_INSTALL_PATH)/lib
LIB       := -L$(CUDA_INSTALL_PATH)/lib64
#LINKLINE  = $(LINK) -o $(TARGET) $(OBJS) $(LIB)
#CUDPPLIB := -lcudpp$(LIBSUFFIX)
#CUDPPLIB := -lcudpp64$(LIBSUFFIX)
LIB += -lcudart ${OPENGLLIB} $(PARAMGLLIB) $(RENDERCHECKGLLIB) $(CUDPPLIB) ${LIB} -ldl -rdynamic

%.cu.o : $(SRCDIR)%.cu $(CU_DEPS)
	$(VERBOSE)$(NVCC) $(NVCCFLAGS) $(SMVERSIONFLAGS) -o $@ -c $<

%.cpp.o : $(SRCDIR)%.cpp $(C_DEPS)
	$(VERBOSE)$(CXX) $(CXXFLAGS) -o $@ -c $<

%.c.o : $(SRCDIR)%.c $(C_DEPS)
	$(VERBOSE)$(CC) $(CFLAGS) -o $@ -c $<

OBJS +=  $(patsubst %.cpp,%.cpp.o,$(notdir $(CXXFILES)))
OBJS +=  $(patsubst %.c,  %.c.o,  $(notdir $(CFILES)))
OBJS +=  $(patsubst %.cu, %.cu.o, $(notdir $(CUFILES)))

$(TARGET): $(OBJS)
	$(VERBOSE)$(LINK) -o $@ $(OBJS) $(LIB)
